package com.iweb.day12;

import java.io.*;

/**
 * @author WuYongheng
 * @date 2021/12/5
 * @description 测试多线程拷贝
 */
public class MyThread2 extends Thread{
    @Override
    public void run() {
        copyFile();
    }

    private void copyFile() {
        long startTime = System.currentTimeMillis();
        String newFileName = Thread.currentThread().getName() + ".txt";

        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\三体3.txt")));
            writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream("D:\\temp\\" + newFileName)));
            String line = reader.readLine();
            while (line != null) {
                for (int i = 0; i < 2000; i++) {
                    writer.write(line);
                }
                writer.newLine();
                writer.flush();
                line = reader.readLine();
            }
            // 结束时间
            long endTime = System.currentTimeMillis();
            System.out.println("复制成功，耗时：" + (endTime - startTime) / 1000.0 + "s");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (reader != null) {
                    reader.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }
}
